package com.hc.sss.cxm.controller;

import com.hc.sss.common.OrderNumberUtil;
import com.hc.sss.pojo.Address;
import com.hc.sss.pojo.Buy;
import com.hc.sss.pojo.Order;
import com.hc.sss.pojo.User;
import com.hc.sss.service.AddressService;
import com.hc.sss.service.BuyService;
import com.hc.sss.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/buy")
public class BuyController {

	@Autowired
	private BuyService buyService;

	@Autowired
	private AddressService addressService;

	@Autowired
	private OrderService orderService;

	/**
	 * 帮我买
	 */
	@RequestMapping("/add")
	public int addBuy(@RequestParam Map buyMap, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");

		OrderNumberUtil orderNumberUtil = new OrderNumberUtil();
		String AssignmentNumber = "buy" + orderNumberUtil.getOrderCode(111);// 任务编号
		String orderNumber = "buyOrder" + orderNumberUtil.getOrderCode(111);// 订单编号
		String addressNumber = "addrNumber" + orderNumberUtil.getOrderCode(111);// 发送地址编号
		String addressDeliverNumber = "addrDeliverNumber" + orderNumberUtil.getOrderCode(111);// 接受地址编号

		buyMap.put("buyId", AssignmentNumber);// 存入任务id
		buyMap.put("buyCreateTime", new Date());// 创建时间
		buyMap.put("buyStatus", 1);// 状态 1-正常
		buyMap.put("buyOrderId", orderNumber);// 存入订单id

		// 把信息存入地址表
		// 1.存入第一个地址，发送地址
		Address address = new Address();
		address.setAddrId(addressNumber);// 设置发送地址编号
		address.setAddrName((String) buyMap.get("buyAddr"));// 买的地址
		address.setAddrCreateTime(new Date());// 创建时间
		address.setAddrStatus(1);// 地址状态(1正常,-1删除)
		addressService.addAddres(address);

		// 2.存入第二个地址，接受地址
		Address addressDeliver = new Address();
		addressDeliver.setAddrId(addressDeliverNumber);// 设置接受地址编号
		addressDeliver.setAddrName((String) buyMap.get("buyDeliverAddr"));// 接受地址
		addressDeliver.setAddrCreateTime(new Date());// 创建时间
		addressDeliver.setAddrStatus(1);// 地址状态(1正常,-1删除)
		addressService.addAddres(addressDeliver);

		// 通过地址获得地址表的id，存到任务表里面
		buyMap.put("buyAddr", address.getAddrId());// 把发送地址id存到任务表内
		buyMap.put("buyDeliverAddr", addressDeliver.getAddrId());// 把接受地址id存到任务表内

		int payOption = -1;//支付方式
		if (buyMap.get("payOption") != null) {
			if ("支付宝".equals(buyMap.get("payOption"))) {
				payOption = 1;
			} else if ("微信".equals(buyMap.get("payOption"))) {
				payOption = 2;
			} else if ("银行卡".equals(buyMap.get("payOption"))) {
				payOption = 3;
			} else if ("余额".equals(buyMap.get("payOption"))) {
				payOption = 4;
			}
		}
		// 把信息存入订单表
		Order order = new Order();
		order.setOrderId(orderNumber);
		order.setOrderMoney(BigDecimal.valueOf(100));// 由于没有算法算金额，所以金额默认都是100
		order.setOrderPreferentialMoney(BigDecimal.valueOf(0));// 优惠金额默认是0
		order.setOrderPay(payOption);
		order.setOrderCreateTime(new Date());// 创建时间
		order.setOrderAnnotation((String) buyMap.get("orderAnnotation"));// 存入订单备注信息
		orderService.insertOrder(order);

		
		return buyService.insertBuy(buyMap);
	}

}
